package com.yd.app.mapper;

import com.yd.app.mapper.provider.OldsRetrieveProvider;
import com.yd.domain.business.Old;
import com.yd.domain.searchParam.OldParam;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @author zy
 * @create 2018-03-08 10:27
 * @desc 旧物管理
 **/
@Repository
public interface OldsRetrieveMapper {
    /**
     * @Author: zy
     * @Desc: 录入基本旧物的基本信息
     * @Date:  2018/3/8
     * @Param
     */
    @Insert("INSERT INTO business_old (id,addUser,`status`,checkTime,checkUser,createTime,dealStatus,des,ifPic,perWeight,price,title,type,dealTime,remark,iconUrl )" +
            " VALUES(#{o.id},#{o.addUser},#{o.status},#{o.checkTime},#{o.checkUser},#{o.createTime},#{o.dealStatus},#{o.des},#{o.ifPic},#{o.perWeight},#{o.price}," +
            "#{o.title},#{o.type},#{o.dealTime},#{o.remark},#{o.iconUrl})")
    void addOld(@Param("o")Old o);

    /**
     * @Author: zy
     * @Desc: 修改数据
     * @Date:  2018/3/8
     * @Param
     */
    @Update("update business_old set `status`=#{o.status},checkTime=#{o.checkTime},checkUser=#{o.checkUser},createTime=#{o.createTime},dealStatus=#{o.dealStatus}," +
            "des=#{o.des},ifPic=#{o.ifPic},perWeight=#{o.perWeight},price=#{o.price},title=#{o.title},type=#{o.type},dealTime=#{o.dealTime},remark=#{o.remark},iconUrl=#{o.iconUrl}  where id=#{o.id} ")
    void updateOld(@Param("o")Old o);
    /**
     * @Author: zy
     * @Desc: 删除数据
     * @Date:  2018/3/8
     * @Param
     */
    @Delete("delete from business_old where id=#{id}")
    void deleteOld(@Param("id") String id);
    /**
     * @Author: zy
     * @Desc: 分页查询数据
     * @Date:  2018/3/8
     * @Param
     */
    @SelectProvider(method = "searchOldList",type= OldsRetrieveProvider.class)
    List<Old> searchOldList(@Param("oldParam") OldParam oldParam, @Param("startRow") int startRow, @Param("pageSize") int pageSize);
    /**
     * @Author: zy
     * @Desc: 查询数量
     * @Date:  2018/3/7
     */
    @SelectProvider(method = "searchOldCount",type= OldsRetrieveProvider.class)
    int searchOldCount(@Param("oldParam") OldParam oldParam);

    /**
     * 通过id查旧物
     * @param id
     * @return
     */
    @Select(" select o.id,d_sta.title as statusName,d_del.title AS dealStatusName,dic.typeName as typeName ,cu.realName as checkUserName," +
            "addu.realName as addUserName,o.addUser,o.`status`,o.checkTime,o.checkUser,o.createTime,o.dealStatus,o.des,o.ifPic,o.perWeight,o.price,o.title,o.type,o.dealTime,o.remark,o.iconUrl  " +
            " FROM business_old o LEFT JOIN business_gType dic ON dic.id=o.type " +
            " LEFT JOIN sys_dic d_sta ON d_sta.`code`=o.`status` and d_sta.type='oldStatus'" +
            " LEFT JOIN sys_dic d_del ON d_del.`code`=o.dealStatus and d_del.type='oldDelSta'" +
            " LEFT JOIN sys_user addu ON o.addUser=addu.id" +
            " LEFT JOIN sys_user cu ON o.checkUser=cu.id" +
            " WHERE o.id=#{id}")
    Old getOldById(@Param("id") String id);

    /**
     * @Author: zy
     * @Desc: 修改旧物状态:审核 ,并且设置价格等
     * 审核人、审核时间
     * @Date:  2018/3/8
     * @Param
     */
    @Update("update business_old set `status`=#{o.status},checkTime=#{o.checkTime},checkUser=#{o.checkUser}," +
            "price=#{o.price},remark=#{o.remark}  where id=#{o.id} ")
    void updateStaus(@Param("o")Old o);
    /**
     * @Author: zy
     * @Desc:交易状态 交易时间、最终价格，以及回复内容作为一个交易备注
     * @Date:  2018/3/8
     * @Param
     */
    @Update("update business_old set dealStatus=#{o.dealStatus}, dealTime=#{o.dealTime},price=#{o.price},remark=#{o.remark} where id=#{o.id}")
    void updateDealStaus(@Param("o")Old o);

    /**
     * 根据用户查询"我的旧物"
     * @Param oldParam
     * @Param startRow
     * @Param pageSize
     * @return
     */
    @SelectProvider(method = "searchMyOldList",type= OldsRetrieveProvider.class)
    List<Old> searchMyOld(@Param("oldParam") OldParam oldParam, @Param("startRow") int startRow, @Param("pageSize") int pageSize);
    /**
     * @Author: zy
     * @Desc: 根据id 查询旧物
     * @Date:  2018/4/13
     * @Param
     */
    @Select("select o.*,u.realName as checkUserName from business_old o LEFT JOIN sys_user u on o.checkUser=u.id where o.id=#{id}")
    Old getOldByID(@Param("id")String id);

    /**
     * 审核旧物状态
     * @param oldId
     * @param checkStatus
     * @param checkUser
     * @param checkTime
     */
    @Update("update business_old set status=#{checkStatus},checkUser=#{checkUser},checkTime=#{checkTime} where id=#{oldId} ")
    void updateOldStatus(@Param("oldId") String oldId, @Param("checkUser") String checkUser, @Param("checkTime") Date checkTime, @Param("checkStatus")  String checkStatus);

    /**
     * 旧物估价
     * @param old
     */
    @Update("update business_old set price=#{old.price},remark=#{old.remark} where id=#{old.id} ")
    void assessOld(@Param("old")Old old);

    /**
     * 旧物交易状态
     * @param oldId
     * @param dealStatus
     * @param dealTime
     */
    @Update("update business_old set dealStatus=#{dealStatus},dealTime=#{dealTime} where id=#{oldId} ")
    void updateDealStatus(@Param("oldId") String oldId, @Param("dealTime") Date dealTime, @Param("dealStatus")  String dealStatus);

    /**
     * 删除图片
     * @param id
     */
    @Update("update business_old set iconUrl='',ifPic=0 where id=#{id} ")
    void updateIconUrl(@Param("id") String id);
}
